LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 12-23-2004, 05:01 AM   #1
Dee-ehn
Member
 
Registered: Apr 2004
Distribution: Suse 9 pro
Posts: 52

Rep: Reputation: 15
[N00B] Generate filenames from a file!


Hi,

I have a file (let's call it a.gif). And I have a text file with a list of names in it.
What I need is some kind of script that will make a copies of a.gif; each new name of a.gif will correspond to 1 line from that text file.

So I get for instance 5000 copies of a.gif, called house.gif, car.gif, tux.gif and so on.

How can I realise this? I think a simple BASH script can do this, but I do not have a clue how


Thanx in advnace!
 
Old 12-23-2004, 05:09 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Puppy
Posts: 3,048

Rep: Reputation: 95
seems a strange thing to want to do!

what for?

billy
 
Old 12-23-2004, 05:21 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,524

Rep: Reputation: 93
Quote:
Originally posted by bigearsbilly
seems a strange thing to want to do!
I agree. Anyways, here goes:

Example list file "yourlist.txt":
Code:
blabla.gif
/home/hko/dummy/b.gif
/home/hko/dummy/c.gif
/home/hko/dummy/d.gif
/tmp/dummy/aaa.gif
/tmp/dummy/bbb.gif
/tmp/dummy/ccc.gif
The (bash) script "multicopy.sh":
Code:
#!/bin/bash

# This script copies SRCFILE (see below) to all
# file names listed in LISTFILE.
#
# When a directory in LISTFILE does not exist yet,
# it will be created.

SRCFILE="a.gif"
LISTFILE="yourlist.txt"

while read NEWFILE ; do
	mkdir -p "${NEWFILE%/*}"
	cp -v $SRCFILE $NEWFILE
done <"$LISTFILE"
exit 0
 
Old 12-23-2004, 05:24 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Puppy
Posts: 3,048

Rep: Reputation: 95
Very nice, neat bit of code Hko.
let's hope it will be used for a good cause!

regards,
billy
 
Old 12-23-2004, 05:48 AM   #5
Dee-ehn
Member
 
Registered: Apr 2004
Distribution: Suse 9 pro
Posts: 52

Original Poster
Rep: Reputation: 15
Hko: many thanx, I'll be trying it soon!

What I do need it for? For a website that I am devoping. I'm, working with a huge set of items that has to be accomponied with the same banner everytime. When I do this the banner has the same name as the variable for the showed item so I only need 1 variable. In that way, if we want to, we can later change individual banners without having to rewrite the entire code; for now we'll just use one banner... that's why
 
Old 12-23-2004, 02:49 PM   #6
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 53
Quote:
Very nice, neat bit of code Hko.
I agree but am confused as why are you making all of those directories?

The way it looks to me is the name file has a bunch of names ....

list.txt
Code:
car
house
train
tux
fish
bear
truck
and your script would be like this ...
Code:
#!/bin/bash
# This script copies SRCFILE (see below) to all
# file names listed in LISTFILE.

SRCFILE="a.gif"
LISTFILE="list.txt"

while read NEWFILE ; do
	cp -v $SRCFILE ${NEWFILE}.gif
done <"$LISTFILE"
exit 0
 
Old 12-23-2004, 05:09 PM   #7
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,524

Rep: Reputation: 93
Quote:
Originally posted by homey
I agree but am confused as why are you making all of those directories?

The way it looks to me is the name file has a bunch of names ....
It's just a feature,
With your list.txt my script wil have the same result.
I was more or less expecting some follow-up question like: "how to copy the files to other directories?", and possibly next: "how to copy them to a directory that doesn't exist yet?".
 
Old 12-23-2004, 07:27 PM   #8
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 53
Cool!
I wonder if there is a performance advantage to using this ...
done <"$LISTFILE"

instead of this ....
cat $LISTFILE |\
while read NEWFILE ; do


or is it just a method which you prefer?
 
Old 12-24-2004, 03:19 AM   #9
dustu76
Member
 
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153

Rep: Reputation: 30
Hko, since you seem to be one who likes to take care of the widest range of scenarios, I thought I would point something to you:

Code:
$ ls -l
total 0
-rw-r--r--   1 supmis   supmis         0 Dec 24 14:35  asdf  asdf
-rw-r--r--   1 supmis   supmis         0 Dec 24 14:35 a
-rw-r--r--   1 supmis   supmis         0 Dec 24 14:35 asdf asasd23
-rw-r--r--   1 supmis   supmis         0 Dec 24 14:35 b
$ ls -1 > /tmp/a.txt
$ cat /tmp/a.txt
 asdf  asdf
a
asdf asasd23
b
$ while read fname ; do
> echo "${fname}"
> done < /tmp/a.txt
asdf  asdf
a
asdf asasd23
b
$ for fname in "$(cat /tmp/a.txt)" ; do
> echo "${fname}"
> done
 asdf  asdf
a
asdf asasd23
b
$

The point of this code is the leading space for filename " asdf asdf". In this case, for seems to be a better choice. This happends on those extremely rare occassions when the filename has leading space(s).

HTH.
 
Old 12-24-2004, 05:57 AM   #10
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,524

Rep: Reputation: 93
Quote:
Originally posted by dustu76
Hko, since you seem to be one who likes to take care of the widest range of scenarios,
Heh..well yes, I guess so...

Quote:
I thought I would point something to you:

[..snip code..]

The point of this code is the leading space for filename " asdf asdf". In this case, for seems to be a better choice. This happends on those extremely rare occassions when the filename has leading space(s).
Good point! Though, as you say, it's a rare occasion. A bug that counts anyways. Thanks.

I wonder if it could be done right without the "for F in $(cat file.txt) ; do" construction. Generally I don't like this very much, because it can yield in a huge command line being executed.
 
Old 12-24-2004, 06:23 AM   #11
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Puppy
Posts: 3,048

Rep: Reputation: 95
or

DATA
Code:
billym.primadtpdev>cat ~/1
  asdf  asdf
a
asdf asasd23
b
SCRIPT
Code:
billym.primadtpdev>cat ~/2

IFS=
while read file
    do
        echo "file=[$file]"
done

RESULT

Code:
billym.primadtpdev>bash ~/2 < ~/1
file=[  asdf  asdf]
file=[a]
file=[asdf asasd23]
file=[b]
 
Old 12-24-2004, 07:21 AM   #12
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,524

Rep: Reputation: 93
Ah, IFS,...right. Thanks.
 
Old 12-28-2004, 10:06 AM   #13
Dee-ehn
Member
 
Registered: Apr 2004
Distribution: Suse 9 pro
Posts: 52

Original Poster
Rep: Reputation: 15
Thanx, it worked out great!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
KDE 3.3.0 file manager doesn't show long filenames grayswander Linux - General 2 11-06-2004 06:50 AM
How may I tell gcc to generate an assembly file? LongName Programming 4 08-30-2004 02:31 AM
generate crc value for file(s) ? Apostasy Linux - Newbie 17 07-09-2004 12:15 PM
Perl Script Reading a txt file and generate html to be published! kofi Linux - Software 1 09-22-2003 05:12 PM
How can I find/generate a kernel .config file? David Reid Linux - Newbie 5 08-08-2002 05:30 AM


All times are GMT -5. The time now is 02:45 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration